package com.jin.utils;


import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class TestT {
    public static void main(String[] args) throws Exception{
        PdfReader pdfReader = new PdfReader("D:\\资料\\6-新希望\\合同模板\\超e贷\\20240924\\最高额保证合同-测试版本-域名版本-GEN.pdf");
        PdfWriter pdfWriter = new PdfWriter("D:\\资料\\6-新希望\\合同模板\\超e贷\\20240924\\最高额保证合同-测试版本-域名版本-GEN11.pdf");

        PdfDocument pdfDocument = new PdfDocument(pdfReader,pdfWriter);

        PdfAcroForm acroForm = PdfAcroForm.getAcroForm(pdfDocument, false);
        Map<String, PdfFormField> formFields = acroForm.getFormFields();
        Set<String> set = new HashSet<>();
        for (String key : formFields.keySet()) {

            // 报错时打印key
            PdfFormField pdfFormField = formFields.get(key);

            System.out.println(key);
            pdfFormField.setValue("陈爽");
        }
        acroForm.flattenFields();
        pdfDocument.close();
    }
}
